package com.dsr.service.impl;

import cn.hutool.core.date.DateTime;
import com.dsr.bean.SectionBean;
import com.dsr.consts.PageResult;
import com.dsr.consts.Result;
import com.dsr.dao.SectionDao;
import com.dsr.entity.Section;
import com.dsr.service.SectionService;
import com.dsr.util.IdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Section)表服务实现类
 *
 * @author dsr
 * @since 2020-08-13
 */
@Service("sectionService")
@Transactional
public class SectionServiceImpl implements SectionService {
    @Autowired
    private SectionDao sectionDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    public Result getEntity(String id) {
        Section entity = sectionDao.selectByPrimaryKey(id);
        return Result.success(entity);
    }

    @Override
    public Result save(Section section) {
        section.setId(idWorker.nextId() + "");
        section.setDel(0);
        section.setCreateDate(DateTime.now());
        if (StringUtils.isEmpty(section.getParentId())) {
            section.setParentId("0");
        }
        sectionDao.insertSelective(section);
        return Result.success("保存成功");
    }

    @Override
    public Result update(Section section) {
        section.setUpdateDate(DateTime.now());
        sectionDao.updateByPrimaryKeySelective(section);
        return Result.success("修改成功");
    }

    @Override
    public Result delete(String id) {
        if (StringUtils.isNotEmpty(id)) {
            sectionDao.deleteSection(id);
            return Result.success("删除成功");
        }
        return Result.error("请选择要删除的行");
    }

    @Override
    public Result query(int page, int size, Section section) {
        PageHelper.startPage(page, size);
        List<Section> entityList = sectionDao.select(section);
        PageInfo<Section> pageInfo = new PageInfo<>(entityList);
        return Result.success(new PageResult<Section>(pageInfo.getTotal(), pageInfo.getList()));
    }

    @Override
    public Result list() {
        return Result.success(getTreeUtil());
    }

    @Override
    public Result listChildren() {
        return Result.success(sectionDao.listChildren());
    }

    private List<SectionBean> getTreeUtil() {
        List<SectionBean> sectionBeanList = new ArrayList<>();
        List<SectionBean> sectionTree = sectionDao.getSectionTree();
        Map<String, List<SectionBean>> collect =
                sectionTree.stream()
                        .filter(item -> !"0".equals(item.getParentId()))
                        .collect(Collectors.groupingBy(SectionBean::getParentId));
        sectionTree.forEach(
                item -> {
                    item.setValue(item.getId());
                    if (collect.containsKey(item.getId())) {
                        item.setChildren(collect.get(item.getId()));
                    }
                    if (item.getChildren().size() == 0) {
                        item.setChildren(null);
                    }
                });
        List<SectionBean> children =
                sectionTree.stream()
                        .filter(item -> "0".equals(item.getParentId()))
                        .collect(Collectors.toList());
        SectionBean base = new SectionBean();
        base.setId("0");
        base.setParentId("0");
        base.setLabel("顶级类别");
        base.setChildren(children);
        sectionBeanList.add(base);
        return sectionBeanList;
    }
}
